package com.lightheart.sphr.xiaobinggan.timeselection.ui

import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.base.BaseActivity
import com.lightheart.sphr.xiaobinggan.module.home.presenter.HomePresenter
import com.lightheart.sphr.xiaobinggan.timeselection.listener.TimerListener
import com.lightheart.sphr.xiaobinggan.timeselection.uitls.CustomToastUtils
import com.lightheart.sphr.xiaobinggan.timeselection.uitls.TimerManager
import kotlinx.android.synthetic.main.common_toolbar.*
import kotlinx.android.synthetic.main.layout_dou.*
import java.util.*

class TimeIntegralActivity : BaseActivity<HomePresenter>(), TimerListener {

    private lateinit var animDou: Animation
    private lateinit var animScore: Animation
    private var timer: Timer? = null
    private var task: TimerTask? = null
    private var minuteTime = 0
    private var startMinuteTime = 0
    private var isTouch: Boolean = true

    override fun getLayoutId() = R.layout.activity_time_integral

    override fun initInjector() {
    }

    override fun initView() {
        initToolbar(common_toolbar, common_toolbar_title_tv, bt_sub, R.string.time_integral, false, R.string.register)

//        tv_start.setOnClickListener {
//            TimerManager.instance.startTimer()
//        }
//
//        tv_stop.setOnClickListener {
//            TimerManager.instance.stopTimer(c_progress.progress)
//        }

    }

    override fun onStart() {
        super.onStart()
        rl_dou.visibility = View.VISIBLE
        c_progress.max = 30
        minuteTime = 0
        startTimer()
        TimerManager.instance.setListener(this).startTimer()
        initAnim()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {//有按下动作时取消定时
                if (startMinuteTime == 36) {
                    isTouch = false
                } else {
                    isTouch = true
                }
                stopTimer()
            }
            MotionEvent.ACTION_UP -> { //抬起时启动定时
                startTimer()
                if (!isTouch) {
                    TimerManager.instance.setListener(this).startTimer()
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onFinish() {
        showGetScore()
    }

    override fun onTick(progress: Int) {
        c_progress.progress = progress
    }

    private fun showGetScore() {
        tv_score.visibility = View.VISIBLE
        iv_dou_out.visibility = View.VISIBLE
        tv_score.startAnimation(animScore)
        iv_dou_out.startAnimation(animDou)
    }


    private fun initAnim() {
        animDou = AnimationUtils.loadAnimation(this, R.anim.anim_dou_out)
        animScore = AnimationUtils.loadAnimation(this, R.anim.anim_score)
        animDou.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                if (minuteTime == 36) {
                    stopTimer()
                    TimerManager.instance.stopTimer(c_progress.progress)
                } else {
                    iv_dou_out.visibility = View.INVISIBLE
                    CustomToastUtils.imgToast("阅读时长", "5")
                    tv_score.visibility = View.GONE
                    iv_dou_out.visibility = View.GONE
                    TimerManager.instance.startTimer()
                }
            }

            override fun onAnimationStart(animation: Animation?) {
            }
        })
        animScore.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                tv_score.visibility = View.INVISIBLE
            }

            override fun onAnimationStart(animation: Animation?) {
            }
        })
    }


    override fun onPause() {
        super.onPause()
        TimerManager.instance.stopTimer(c_progress.progress)
        //当activity不在前台是停止定时
        stopTimer()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopTimer()
        minuteTime = 0
    }

    override fun showHomeAsUp(): Boolean {
        return true
    }


    private fun initTimer() {//dispatchTouchEvent
        // 初始化计时器
        task = MyTask()
        timer = Timer()
    }

    inner class MyTask : TimerTask() {
        override fun run() {
            // 初始化计时器
            runOnUiThread {
                minuteTime++
                startMinuteTime = minuteTime
                Log.d("minuteTime", "" + minuteTime)
                if (minuteTime == 36) {
                    //在这里弹窗然后停止计时
                    CustomToastUtils.imgToast("去那里啦呀", "5")
                    minuteTime = 0
                    stopTimer()
                    TimerManager.instance.stopTimer(c_progress.progress)
                }
            }
        }
    }

    private fun startTimer() {
        //启动计时器
        /**
         * java.util.Timer.schedule(TimerTask task, long delay, long period)：
         * 这个方法是说，delay/1000秒后执行task,然后进过period/1000秒再次执行task，
         * 这个用于循环任务，执行无数次，当然，你可以用timer.cancel();取消计时器的执行。
         */
        initTimer()
        try {
            timer?.schedule(task, 1000, 1000)
        } catch (e: IllegalStateException) {
            e.printStackTrace()
            initTimer()
            timer?.schedule(task, 1000, 1000)
        }
    }

    private fun stopTimer() {
        if (timer != null) {
            timer?.cancel()
        }
        minuteTime = 0
    }

}